package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.foodclient.pojo.Comment;
import com.example.foodclient.pojo.CommentBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CommentDetailActivity extends AppCompatActivity {

    private static final String TAG = "CommentDetailActivity";
    private TextView cmdeNameTv;
    private TextView cmdeDesctv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_detail);
        initView();
        showCommentDetail();
    }

    private void initView() {
        cmdeNameTv = findViewById(R.id.cm_de_food_name_tv);
        cmdeDesctv = findViewById(R.id.cm_de_food_desc_tv);
    }

    private void showCommentDetail() {
        Intent intent = getIntent();
        final String id = intent.getStringExtra("id");
        final int position = intent.getIntExtra("position", 0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    Request request = new Request.Builder()
                            .url("http://192.168.137.1:8083/foodserver/comment/findone/" + id)
                            .method("GET", null)
                            .build();

                    Response response = client.newCall(request).execute();

                    String result = response.body().string();

                    Gson gson = new Gson();

                    final Comment.DataBean data = gson.fromJson(result, Comment.class).getData();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            cmdeNameTv.setText(data.getFoods().getName());
                            cmdeDesctv.setText(data.getComment());
                        }
                    });
                    Log.e(TAG, "run: " + result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
